package com.hellobike.haxing.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.order.entity.MonthReward;
import com.hellobike.haxing.modules.order.entity.MonthRewardDetail;
import com.hellobike.haxing.modules.order.mapper.MonthRewardDetailMapper;
import com.hellobike.haxing.modules.order.mapper.MonthRewardMapper;
import com.hellobike.haxing.modules.order.service.IMonthRewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 月度达成奖励设置
 * @Author: haxing-boot
 * @Date: 2020-01-08
 * @Version: V1.0
 */
@Service
public class MonthRewardServiceImpl extends ServiceImpl<MonthRewardMapper, MonthReward> implements IMonthRewardService {

    @Autowired
    private MonthRewardDetailMapper monthRewardDetailMapper;

    @Override
    public void saveInfo(MonthReward monthReward) {

        monthReward.setBeginTime(getFirstDayOfMonth(monthReward.getBeginTime()));
        monthReward.setEndTime(getLastDayOfMonth(monthReward.getEndTime()));
        super.save(monthReward);

        if (monthReward.getStepOneAmount() != null && monthReward.getStepOneReward() != null) {
            MonthRewardDetail detail = new MonthRewardDetail();
            detail.setLevel(1);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepOneAmount());
            detail.setRewardAmount(monthReward.getStepOneReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepTwoAmount() != null && monthReward.getStepTwoReward() != null) {
            MonthRewardDetail detail = new MonthRewardDetail();
            detail.setLevel(2);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepTwoAmount());
            detail.setRewardAmount(monthReward.getStepTwoReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepThreeAmount() != null && monthReward.getStepThreeReward() != null) {
            MonthRewardDetail detail = new MonthRewardDetail();
            detail.setLevel(3);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepThreeAmount());
            detail.setRewardAmount(monthReward.getStepThreeReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepFourAmount() != null && monthReward.getStepFourReward() != null) {
            MonthRewardDetail detail = new MonthRewardDetail();
            detail.setLevel(4);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepFourAmount());
            detail.setRewardAmount(monthReward.getStepFourReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepFiveAmount() != null && monthReward.getStepFiveReward() != null) {
            MonthRewardDetail detail = new MonthRewardDetail();
            detail.setLevel(5);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepFiveAmount());
            detail.setRewardAmount(monthReward.getStepFiveReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
    }

    @Override
    public void updateInfo(MonthReward monthReward) {

        monthReward.setBeginTime(getFirstDayOfMonth(monthReward.getBeginTime()));
        monthReward.setEndTime(getLastDayOfMonth(monthReward.getEndTime()));
        super.updateById(monthReward);

        monthRewardDetailMapper.deleteByMonthRewardId(monthReward.getId());

        MonthRewardDetail detail = null;
        if (monthReward.getStepOneAmount() != null && monthReward.getStepOneReward() != null) {
            detail = new MonthRewardDetail();
            detail.setLevel(1);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepOneAmount());
            detail.setRewardAmount(monthReward.getStepOneReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepTwoAmount() != null && monthReward.getStepTwoReward() != null) {
            detail = new MonthRewardDetail();
            detail.setLevel(2);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepTwoAmount());
            detail.setRewardAmount(monthReward.getStepTwoReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepThreeAmount() != null && monthReward.getStepThreeReward() != null) {
            detail = new MonthRewardDetail();
            detail.setLevel(3);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepThreeAmount());
            detail.setRewardAmount(monthReward.getStepThreeReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepFourAmount() != null && monthReward.getStepFourReward() != null) {
            detail = new MonthRewardDetail();
            detail.setLevel(4);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepFourAmount());
            detail.setRewardAmount(monthReward.getStepFourReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
        if (monthReward.getStepFiveAmount() != null && monthReward.getStepFiveReward() != null) {
            detail = new MonthRewardDetail();
            detail.setLevel(5);
            detail.setMonthRewardId(monthReward.getId());
            detail.setAmount(monthReward.getStepFiveAmount());
            detail.setRewardAmount(monthReward.getStepFiveReward());
            detail.setCreateBy(monthReward.getCreateBy());

            monthRewardDetailMapper.insert(detail);
        }
    }



    /**
     * 根据当前日期，获取本月第一天的0时0分0秒
     *
     * @param date
     * @return
     */
    private Date getFirstDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND, 0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 本月第一天的时间戳转换为字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date firstDay;
        try {
            firstDay = sdf.parse(sdf.format(new Date(new Long(c.getTimeInMillis()))));
            return firstDay;
//            return sdf.format(firstDay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据当前日期，获取上月最后一天的0时0分0秒
     *
     * @param date
     * @return
     */
    public Date getLastDayOfMonth(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));//设置为最后一天
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        // 本月第一天的时间戳转换为字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date lastDay;
        try {
            lastDay = sdf.parse(sdf.format(new Date(new Long(c.getTimeInMillis()))));
            return lastDay;
//            return sdf.format(lastDay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
